/**实现问题及问题补充实现*/
$(document).ready(function(){
   initQuestion();
});
/********************************************************************************
 函数名：initQuestion
 功能：初始化提问页面
 输入参数: 无
 返回值：无
 创建信息：卢映恒(2015-12-07）
 修改记录：无
 审查人：无
 *******************************************************************************/
function initQuestion() {
    setClickEvent();
    var courseName = G_Prg.getCookie('courseName');//课程名称
    var firstKnowledgePointName = G_Prg.getCookie('firstName');//一级知识点名称
    var firstKnowledgeID = G_Prg.getCookie('firstKnowledgeID');//一级知识点ID
    var secondKnowledgePointName = G_Prg.getCookie('knowledgePointName');//二级知识点名称
    var secondKnowLedgeID = G_Prg.getCookie('secondKnowLedgeID');//二级知识点
    var coursewareID = getQueryString('coursewareID', true);//视频ID
    var videoID = getQueryString('videoID', true);//课件ID
    if (coursewareID) {
        var learningName = getQueryString('coursewareName', true);//课程名称
        var learningID=coursewareID;
    } else {
        learningName = getQueryString('videoName', true);//视频名称
        learningID=videoID;
    }
    //显示分类
    G_Prg.$('firstKnowledgePointName').innerHTML = '<b>' + firstKnowledgePointName + '</b>';
    G_Prg.$('secondKnowledgePointName').innerHTML = '<b>' + secondKnowledgePointName + '</b>';
    G_Prg.$('learningName').innerHTML = '<b>' + learningName + '</b>';
    /********************************************************************************
     函数名：setClickEvent
     功能：指定点击事件
     输入参数: 无
     返回值：无
     创建信息：卢映恒(2015-12-07）
     修改记录：无
     审查人：无
     *******************************************************************************/
    function setClickEvent() {
        //返回
        $(document).on('click', '#returncourseware', function () {
            window.history.go(-1);
        });
        //提交问题
        $(document).on('click', '#submitQuestion', function () {
            submitQuestion();
        });
        //跳转一级知识点对应页面
        $(document).on('click', '#firstKnowledgePointName', function () {
            location.href = 'knowledge.html?knowledgePointID=' + firstKnowledgeID;
        });
        //跳转二级知识点对应页面
        $(document).on('click', '#secondKnowledgePointName', function () {
            location.href = 'courseWare.html?knowledgePointID=' + secondKnowLedgeID;
        })
    }


    /********************************************************************************
     函数名：submitQuestion
     功能：提交问题
     输入参数: 无
     返回值：无
     创建信息：卢映恒(2015-12-07）
     修改记录：无
     审查人：无
     *******************************************************************************/
    function submitQuestion() {
        var title = $('#title').val();
        var additional = $('#additional').val();
        if (!title) {
            G_Popup.alert('提示', '请填写“一句话描述你的疑问”。');
        } else if (title && title.length > 15) {
            G_Popup.alert('提示', '“一句话描述你的疑问”不能超过15字。')
        } else {
            //添加到数据库
            var url = '/student/question/save';
            if(coursewareID){
                var param = {"title":title,"additional":additional,"coursewareID":coursewareID};
            }else{
                param={"title":title,"additional":additional,"videoID":videoID};
            }
            var success=function(json){
                var jsonData=JSON.parse(json);
                if(jsonData.status===200){
                    G_Popup.alert('温馨提示','您的问题已提交。','确定',function(){
                        if(coursewareID){
                            location.href='coursewarePlayer.html?coursewareID='+coursewareID+'&coursewareName='+learningName;
                        }else{
                            location.href='detailInfo.html?videoID='+videoID+'&videoName='+learningName;
                        }
                    });
                }else{
                    G_Popup.alert('温馨提示',jsonData.msg);
                }
            };
            G_AjaxApi.post(url,param,true,success);


        }


    }

    /********************************************************************************
     函数名：getQueryString
     功能：获取url参数
     输入参数: name获取参数名称，iframe为true 则获取当前iframe内的url内的参数
     返回值：无
     创建信息：姜勇军(2015-09-10)
     修改记录：无
     审查人：无
     *******************************************************************************/
    function getQueryString(name, iframe) { //name 参数 return 对应参数的值
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var frame = iframe;
        if (frame) {
            var rechargeString = window.location.search.substr(1).match(reg);
        } else {
            var rechargeString = window.parent.location.search.substr(1).match(reg);
        }
        if (rechargeString != null) {
            return decodeURI(rechargeString[2]);
            return null;
        }
    }
}